package restful;

import java.util.*;

public class MethodsOrGoals {

	public static final List<String> examplesOf(final RestfulProtocol protocol, final String goal) {
		final List<String> examples = new LinkedList<String>();

		for (final RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) {
				for (final RestfulMethod method : resource.getMethods()) {
					examples.addAll(method.getExamples());
				}
			}
		}

		return examples;
	}

	public static final String goalOf(final String queryString) {
		return Methods.methodOf(queryString);
	}

	public static final List<RestfulResource> goalsOf(final RestfulProtocol protocol) {
		return protocol.getResources();
	}

	public static final List<RestfulResource> goalsOf(final RestfulTopic topic) {
		return topic.getResources();
	}

	public static final List<RestfulMethod> methodsOf(final RestfulProtocol protocol, final String goal) {
		for (final RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) return resource.getMethods();
		}
		return null;
	}

	public static final <T> List<String> names(final List<T> list) {
		final List<String> names = new LinkedList<String>();

		if (list.isEmpty()) return names;

		final T first = list.iterator().next();

		if (first instanceof RestfulResource) {
			for (final T next : list) {
				names.add(((RestfulResource) next).getName());
			}
		}

		if (first instanceof RestfulTopic) {
			for (final T next : list) {
				names.add(((RestfulTopic) next).getName());
			}
		}

		return names;
	}

	public static final List<RestfulParameter> optionalParametersOf(final RestfulProtocol protocol, final String goal) {
		final List<RestfulParameter> parameters = new LinkedList<RestfulParameter>();

		for (final RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) {
				for (final RestfulMethod method : resource.getMethods()) {
					parameters.addAll(method.getOptionalParameters());
				}
			}
		}

		return parameters;
	}

	public static final <T> List<String> patterns(final List<RestfulMethod> methods) {
		final List<String> names = new LinkedList<String>();
		for (final RestfulMethod method : methods) {
			names.add(method.getPattern());
		}
		return names;
	}

	public static final List<RestfulParameter> requiredParametersOf(final RestfulProtocol protocol, final String goal) {
		final List<RestfulParameter> parameters = new LinkedList<RestfulParameter>();

		for (final RestfulResource resource : protocol.getResources()) {
			if (resource.getName().equals(goal)) {
				for (final RestfulMethod method : resource.getMethods()) {
					parameters.addAll(method.getRequiredParameters());
				}
			}
		}

		return parameters;
	}

	public static final List<RestfulResource> resourcesOf(final RestfulProtocol protocol) {
		return goalsOf(protocol);
	}

	public static final RestfulTopic topic(final RestfulProtocol protocol, final String topicName) {
		for (final RestfulTopic topic : protocol.getTopics()) {
			if (topic.getName().equals(topicName)) return topic;
		}
		return null;
	}

	public static final List<RestfulTopic> topicsOf(final RestfulProtocol protocol) {
		return protocol.getTopics();
	}
}
